package com.dap.cms.governance.controller;

import com.dap.cms.governance.po.CmsComponentInfoEnvPo;
import com.dap.cms.governance.po.CmsComponentInfoPo;
import com.dap.cms.governance.service.CmsComponentInfoEnvService;
import com.dap.cms.governance.service.CmsComponentInfoService;
import com.dap.dao.model.QueryResult;
import com.fesine.commons.entity.Result;
import com.fesine.commons.enums.ResultEnum;
import com.fesine.commons.util.ResultUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: t_cms_component_info表对应的控制层类
 * @author: Fesine
 * @createTime:2018/08/21
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2018/08/21
 */
@RestController
@RequestMapping("/v1")
public class CmsComponentInfoController {
    private static final Logger logger = LoggerFactory.getLogger(CmsComponentInfoController.class);
    @Autowired
    private CmsComponentInfoService cmsComponentInfoService;
    @Autowired
    private CmsComponentInfoEnvService cmsComponentInfoEnvService;

    /**
     * 新增CmsComponentInfoPo
     *
     * @param cmsComponentInfoPo
     * @return result
     */
    @PostMapping("/cmsComponentInfo")
    public Result addCmsComponentInfo(CmsComponentInfoPo cmsComponentInfoPo) {
        try {
            cmsComponentInfoService.save(cmsComponentInfoPo);
            return ResultUtils.success(ResultEnum.CREATED, null);
        } catch (Exception e) {
            logger.error("新增失败", e);
            return ResultUtils.error(500, "新增失败");
        }

    }

    /**
     * 更新CmsComponentInfoPo
     *
     * @param cmsComponentInfoPo
     * @return result
     */
    @PutMapping("/cmsComponentInfo")
    public Result updateCmsComponentInfo(CmsComponentInfoPo cmsComponentInfoPo) {
        try {
            cmsComponentInfoService.update(cmsComponentInfoPo);
            return ResultUtils.success(ResultEnum.CREATED, null);
        } catch (Exception e) {
            logger.error("更新失败", e);
            return ResultUtils.error(500, "更新失败");
        }

    }

    /**
     * 删除CmsComponentInfoPo
     *
     * @param id
     * @return result
     */
    @DeleteMapping("/cmsComponentInfo/{id}")
    public Result deleteCmsComponentInfo(@PathVariable("id") Integer id) {
        CmsComponentInfoPo cmsComponentInfoPo = new CmsComponentInfoPo();
        cmsComponentInfoPo.setId(id);
        cmsComponentInfoService.delete(cmsComponentInfoPo);
        //同时删除CmsComponentInfoEnvPo中的记录
        CmsComponentInfoEnvPo cmsComponentInfoEnvPo = new CmsComponentInfoEnvPo();
        cmsComponentInfoEnvPo.setComponentInfoId(cmsComponentInfoPo.getId());
        cmsComponentInfoEnvService.delete(cmsComponentInfoEnvPo);
        return ResultUtils.success(ResultEnum.DELETED, null);
    }

    /**
     * 查看CmsComponentInfoPo详情
     *
     * @param id
     * @return result
     */
    @GetMapping("/cmsComponentInfo/{id}")
    public Result getCmsComponentInfo(@PathVariable("id") Integer id) {
        CmsComponentInfoPo cmsComponentInfoPo = new CmsComponentInfoPo();
        cmsComponentInfoPo.setId(id);
        cmsComponentInfoPo = cmsComponentInfoService.get(cmsComponentInfoPo);
        return ResultUtils.success(cmsComponentInfoPo);
    }

    /**
     * 查询CmsComponentInfoPo列表
     *
     * @param cmsComponentInfoPo
     * @return result
     */
    @GetMapping("/cmsComponentInfos")
    public Result listAll(CmsComponentInfoPo cmsComponentInfoPo) {
        List<CmsComponentInfoPo> list = cmsComponentInfoService.listAll(cmsComponentInfoPo);
        return ResultUtils.success(list);
    }

    /**
     * 分页查询CmsComponentInfoPo列表
     *
     * @param cmsComponentInfoPo
     * @param pageIndex
     * @param pageSize
     * @return result
     */
    @GetMapping("/cmsComponentInfos/page")
    public Result listPage(CmsComponentInfoPo cmsComponentInfoPo, @RequestParam(value = "pageIndex",
            required = false, defaultValue = "1")
            Integer pageIndex, @RequestParam(value = "pageSize", required = false, defaultValue =
            "10") Integer pageSize) {
        QueryResult<CmsComponentInfoPo> result = cmsComponentInfoService.listPage(cmsComponentInfoPo, pageIndex, pageSize);
        long total = result.getTotalrecord();
        Map<String, Object> map = new HashMap(16);
        if (total > 0) {
            map.put("totalRecord", total);
            map.put("resultList", result.getResultlist());
        }
        return ResultUtils.success(map);
    }

    /**
     * 根据组件componentId查询所有组件详情
     */
    @GetMapping("/cmsComponentInfos/{componentId}")
    public Result getInfosByComponentId(@PathVariable("componentId") Integer componentId) {
        CmsComponentInfoPo cmsComponentInfoPo = new CmsComponentInfoPo();
        cmsComponentInfoPo.setComponentId(componentId);
        List<CmsComponentInfoPo> list = cmsComponentInfoService.listAll(cmsComponentInfoPo);
        return ResultUtils.success(list);
    }
}

